package org.dromara.study.controller;

import com.baomidou.mybatisplus.core.conditions.interfaces.Func;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.websocket.server.PathParam;
import org.checkerframework.checker.units.qual.A;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.web.core.BaseController;
import org.dromara.study.domain.entity.BookCategory;
import org.dromara.study.domain.vo.BookCategoryVo;
import org.dromara.study.service.BookCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/bookCategory")
public class BookCategoryController extends BaseController {

    @Autowired
    private BookCategoryService bookCategoryService;

    @GetMapping("/page")
    public R<Page<BookCategoryVo>> getCategoryList(PageQuery pageQuery, BookCategoryVo bookCategoryVo) {
        return R.ok(bookCategoryService.getPage(pageQuery, bookCategoryVo));
    }

    @PostMapping("/add")
    public R<Long> addCategory(@RequestBody BookCategory bookCategory) {
        boolean success = bookCategoryService.save(bookCategory);
        if (success) {
            return R.ok(bookCategory.getId());
        } else {
            return R.fail();
        }
    }

    @PostMapping("/update")
    public R<String> updateCategory(@RequestBody BookCategory bookCategory) {
        boolean success = bookCategoryService.updateById(bookCategory);
        if (success) {
            return R.ok("更新成功");
        } else {
            return R.fail();
        }
    }

    @DeleteMapping ("/delete/{id}")
    public R<String> updateCategory(@PathVariable("id") Long id) {
        boolean success = bookCategoryService.removeById(id);
        if (success) {
            return R.ok("删除成功");
        } else {
            return R.fail();
        }
    }
}
